import * as Cesium from '@cesiumjs';
import createODLineAppearance from './createODLineAppearance';
import g3d_createTransmitPolyline from './createTransmitPolyline';
import GeometryODLine from './GeometryODLine';

function g3d_createInstance(id, positions, color /*,width*/) {
  const width = arguments.length > 0x3 && arguments[0x3] !== undefined ? arguments[0x3] : 0x2;
  var instance = new Cesium.GeometryInstance({
    geometry: new GeometryODLine({ // see: _0xaa417d
      positions,
      width,
      'vertexFormat': Cesium.PolylineMaterialAppearance.VERTEX_FORMAT
    }),
    id,
    attributes: {
      'color': Cesium.ColorGeometryInstanceAttribute.fromColor(color || Cesium.Color.WHITE),
      'twp': new Cesium.GeometryInstanceAttribute({
        'componentDatatype': Cesium.ComponentDatatype.FLOAT,
        'componentsPerAttribute': 4,
        'normalize': false,
        'value': [0x0, 0x32, 0x1, 0x1]
      })
    }
  });
  return instance;
}

var _0x409b1d = new Cesium['Cartesian4'](0x0, 0x0, 0x0, 0x0);
var _0x416add = new Cesium['Cartesian4'](0x0, 0x0, 0x0, 0x0);
var _0x5b2c54 = {};
_0x5b2c54.timeRatio = 0x0;
_0x5b2c54.repeat = 0x1;
_0x5b2c54.color = [0x1, 0x1, 0x1, 0x1];

function createODLinesPrimitive(data, $color, updateTimeRatio) {
  var translucent = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : !![];
  var image = arguments[4];
  var geometryInstances = [];
  data.forEach(function(e, i) {
    const {
      color,
      width,
      positions,
      startPos,
      endPos,
      heightRatio
    } = e;
    var coords = (positions && positions.map(x => Cesium.Cartesian3.fromRadians.apply(Cesium.Cartesian3, x)));
    coords = coords || g3d_createTransmitPolyline(startPos, endPos, heightRatio);
    var eColor = color && new Cesium.Color(Cesium.Cartesian4.fromArray(color)) || undefined;
    var instance = g3d_createInstance(i, coords, eColor, width);
    geometryInstances.push(instance);
  });

  const appearance = createODLineAppearance($color, translucent, image)
  var primitive = new Cesium.Primitive({
    geometryInstances,
    appearance,
    asynchronous: false
  });

  primitive.update = function(frameState) {
    var _this = this;
    if (typeof _this._batchTable !== 'undefined') {
      var twp = _this._batchTableAttributeIndices.twp;
      var color = _this._batchTableAttributeIndices.color
      var numberOfInstances = _this._batchTable._numberOfInstances;
      for (var instanceIndex = 0; instanceIndex < numberOfInstances; ++instanceIndex) {
        _0x5b2c54.timeRatio = 0;
        _0x5b2c54.repeat = 1;
        Cesium.Cartesian4.fromElements(1, 1, 1, 1, _0x5b2c54.color);
        var _0x295e54 = updateTimeRatio(instanceIndex, frameState, _0x5b2c54),
          _0x15815a = _0x295e54.timeRatio,
          _0x3f9c3c = _0x295e54.repeat,
          _0x40d680 = _0x295e54.color;
        _0x409b1d['x'] = _0x15815a;
        _0x409b1d['y'] = _0x3f9c3c;
        _this._batchTable.setBatchedAttribute(instanceIndex, twp, _0x409b1d);
        _0x416add['x'] = 0xff * _0x40d680[0x0];
        _0x416add['y'] = 0xff * _0x40d680[0x1];
        _0x416add['z'] = 0xff * _0x40d680[0x2];
        _0x416add['w'] = 0xff * _0x40d680[0x3];
        _this._batchTable.setBatchedAttribute(instanceIndex, color, _0x416add);
      }
    }
    Cesium.Primitive.prototype.update.call(_this, frameState);
  };

  return primitive;
}

export default createODLinesPrimitive;
